<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Product extends Controller_Index_Base
{    
    public function action_index()
    {
        $product_id = $this->request->param('id') ? $this->request->param('id') : 0;
        $url        = $this->request->param('url') ? $this->request->param('url') : 0;
        
        if (HTTP_Request::POST == $this->request->method()) {
            $name  = $this->request->post('name');
            $phone = $this->request->post('phone');
            
            $subid = 'nosubid';
            if (isset($_COOKIE['subid']) && $_COOKIE['subid']) {
                $subid = str_replace('|', '', $_COOKIE['subid']);    
            }
            
            file_put_contents(APPPATH.'../data/contacts.txt', $product_id."\r\n".$subid."\r\n".$name."\r\n".$phone."\r\n\r\n", FILE_APPEND);
        }
        
        $product  = ORM::factory('product')->filter($product_id);
        
        $category = ORM::factory('category', $product->category_id);
        if ($category->loaded()) {
            Helper_Index::redirect_301(Helper_Index::catalog_url($category));   
        }
        
        if (parse_url(Helper_Index::product_url($product), PHP_URL_PATH) != '/product/'.$product_id.'-'.$url) {
            Helper_Index::redirect_301(Helper_Index::product_url($product));
        }
        $product_data = $product->get_product_data();
        
        $category = ORM::factory('category', $product->category_id);
        $shop     = ORM::factory('shop', $product->shop_id);
        
        $coupons     = ORM::factory('coupon')->get_coupons_by_product($product);
        $best_coupon = null;
         if (count($coupons)) {
            //$tmp_best_coupon = array_slice($coupons, 0, 1, true);    
            $array_keys = array_keys($coupons);
            $best_coupon = $coupons[$array_keys[0]]; 
        }
        
        $content = new View('index/product/index_tpl');
        $content->product  = $product;
        $content->product_data = $product_data;
        $content->specs    = $product->extract_specs($product_data);
        $content->category = $category;
        $content->shop     = $shop; 
        $content->shop_reviews = Model_Shop_Review::get_reviews($shop, 1);
        $content->shop_videoreviews = $shop->get_videoreviews();
        $content->breadcrumbs = $category->get_breadcrumbs();
        $content->coupons  = $coupons;
        $content->best_coupon = $best_coupon;
        $all_coupons = ORM::factory('coupon')->get_coupons($shop); 
        $content->all_coupons = array_slice($all_coupons, 0, 6);
        $tags = ORM::factory('tag')->get_tags_by_product($product);
        $content->tags     = $tags;
        $content->regions  = ORM::factory('region')->get_rand_regions(count($tags));
        
        $differentcolor_products = $product->get_differentcolor_products();
        $related_products = $product->get_related_products(24);
        $content->differentcolor_products = $differentcolor_products;
        $recommend_products = ORM::factory('product')->get_products($category, null, null, null, null, 1);
        $recommend_products = array_slice($recommend_products, 0, 16);
        $content->recommend_products = $recommend_products;
        $content->related_products_1 = array_slice($related_products, 0, 8);
        $content->related_products_2 = array_slice($related_products, 7, 8);
        $content->related_products_3 = array_slice($related_products, 15, 8);
        $content->related_products_coupons = ORM::factory('coupon')->get_coupons_by_products(array_merge($related_products, $differentcolor_products, $recommend_products));
        $content->related_products_shops   = ORM::factory('shop')->get_shops_by_products(array_merge($related_products, $differentcolor_products, $recommend_products));
        
        $title       = Helper_Index::get_product_index_title($product, $product_data, $best_coupon, $shop); 
        $description = Helper_Index::get_product_index_description($product, $product_data, $best_coupon, $shop);
        $this->print_template($title, $content, $description); 
    }
    
    public function action_go()
    {
        $product_id = $this->request->param('product_id') ? $this->request->param('product_id') : 0;
        $product    = ORM::factory('product', $product_id);
        $shop       = ORM::factory('shop', $product->shop_id); 
        
        $content = new View('index/product/go_tpl');
        $content->title   = $product->name;
        $content->product = $product;
        $content->shop    = $shop;      
        $this->response->body($content);
    }
    
    public function action_out()
    {
        $product_id = $this->request->param('product_id') ? $this->request->param('product_id') : 0;
        $product    = ORM::factory('product', $product_id);
        $shop       = ORM::factory('shop', $product->shop_id); 
        
        ORM::factory('click')->add_click_if_cashback_available($shop); 
        
        $content = new View('index/product/out_tpl');
        $content->product = $product;
        $content->shop    = $shop;
        $content->delay   = 2;       
        $this->response->body($content);
    }
} 
